﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WinRtUtility;
using System.Threading.Tasks;
using System.Diagnostics;

namespace WinRtUtility.Test
{
    public class Poco
    {
        public Poco() { }
        public int IntProp { get; set; }
        public string StringProp { get; set; }
    }
    [TestClass]
    public class ObjectStorageHelperTests
    {
        [TestMethod]
        public void SaveObject()
        {
            //Instantiate an object that we want to save
            var myPoco = new Poco() { IntProp = 1, StringProp = "one" };
            //new up ObjectStorageHelper specifying that we want to interact with the Local storage folder
            var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
            //Save the object (via XML Serialization) to the specified folder, asynchronously
            objectStorageHelper.SaveAsync(myPoco);
        }

        [TestMethod]
        public async void LoadObject()
        {
            //new up ObjectStorageHelper specifying that we want to interact with the Local storage folder
            var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
            //Get the object from the storage folder
            Poco myPoco = await objectStorageHelper.LoadAsync();
        }

        [TestMethod]
        public async void SaveAndLoadPoco()
        {
            var poco = new Poco() { IntProp = 1, StringProp = "one" };
            var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
            
            objectStorageHelper.SaveAsync(poco);
            Task.Delay(TimeSpan.FromSeconds(1)).Wait(); //ensure no handle on the file when objectStorageHelper.LoadASync() tries to access it
            var result = await objectStorageHelper.LoadAsync();

            Assert.AreEqual(poco, result);
        }

        /// <summary>
        /// Check that LoadASync() returns null after a file has been created and then deleted using DeleteASync().
        /// </summary>
        [TestMethod]
        public async void SaveAndDeletePoco()
        {
            var objectStorageHelper = new ObjectStorageHelper<Poco>(StorageType.Local);
            var poco = new Poco() { IntProp = 1, StringProp = "one" };
            objectStorageHelper.SaveAsync(poco);
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            objectStorageHelper.DeleteAsync();
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();

            var result = await objectStorageHelper.LoadAsync();
            Task.Delay(TimeSpan.FromSeconds(1)).Wait();
            
            Assert.AreEqual(result, default(Poco));
        }

        [TestMethod]
        public async void HandleUnSerializableObject()
        {
            var dictionaryObj = new Dictionary<string, string>();
            dictionaryObj.Add("key", "value");
            string errMsg = null;
            try
            {
                var objectStorageHelper = new ObjectStorageHelper<Dictionary<string, string>>(StorageType.Local);
                objectStorageHelper.SaveAsync(dictionaryObj);
            }
            catch (Exception e)
            {
                errMsg = e.Message;
            }
            Assert.AreEqual(true, errMsg.Contains("is not supported because it implements IDictionary"));
        }
    }
}
